* Setting
** run set_paths.do in "do" folder

* Preparing data on Binding measures
*------------------------------------------------------------------------------
*---	Data used in main analyses
* Input: 
* - ${rawdata}imp_detl_yearly_104.dta
* - ${rawdata}mfa8404.dta
* - ${rawdata}hs_mfa_phase_20111208.dta
*
* Output: 
* - ${output_data}quota_mex_weight_hs6.dta

run ${dofiles}construct_binding_measures.do


* Preparing data on exporter-importe match
*------------------------------------------------------------------------------
*---	Data used in main analyses
* Input: 
* - ${rawdata}501_`type'.txt
* - ${rawdata}502_`type'.txt
* - ${rawdata}505_`type'.txt
* - ${rawdata}551_`type'.txt
* - ${rawdata}551_`type'.txt
* - ${rawdata}matched_hs`i'_common.dta
* - ${rawdata}usziplist_zp4.dta
* - ${rawdata}uszipcode_county_wide.dta
* - ${rawdata}uszipmun_state_wide.dta
* Output: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict
* - ${output_data}matched_exp_imp_hs6_junedecember_maquilaprice_restrict
* - ${output_data}501_551_505_frac_50_63_junedecember_maquila_withoutprice
* - ${output_data}exporter_importer_50_63_productlevel_new_junedecember_maquila
* - ${output_data}transportista_rfclist.dta

run ${dofiles}prepare_match_data.do

* Checking the sample selection
*------------------------------------------------------------------------------
* Input: 
* - ${output_data}501_551_505_frac_50_63_junedecember_maquila_withoutprice
* - ${output_data}exporter_importer_50_63_productlevel_new_junedecember_maquila
*
* Output: 
* - ${output}sample_selection/distribution_kept_maquila.png (Figure A1)
* - ${output}sample_selection/distribution_kept_nonmaquila.png (Figure A1)
* - ${output_data}sampleweight.dta

run ${dofiles}check_sample_selection.do


* Preparing data for cutoff and exit
*------------------------------------------------------------------------------
* Input: 
* - ${rawdata}XFEP`year'`month'.DTA
* - ${temp}501_551_`month'`year'use
* - ${output_data}transportista_rfclist.dta
*
* Output: 
* - ${output_data}exp_rfc_prod_textile20002004.dta
* - ${output_data}exp_rfc_prod_textile_us20042011.dta

run ${dofiles}prepare_data_for_exit.do

*------------------------------------------------------------------------------
* Analyzing related to the effects of mfa's end on exports (For Figure 1)
*--- 
* Input: 
* - ${rawdata}chinatousexports19982011.dta
* - ${rawdata}mextousexports19982011.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output}mfa_aggregate/mfa_effects_on_exports.png (Figure 1)
run ${dofiles}analyze_mfa_effects_aggregate.do


* Preparing data on ranking
*------------------------------------------------------------------------------
*---	Data used in main analyses and bin analysis
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
*
* Output: 
* - ${output_data}importer_rank.dta
* - ${output_data}exporter_rank.dta　
* - ${output_data}importer_rank5.dta
* - ${output_data}importer_rank10.dta
* - ${output_data}importer_rank20.dta
* - ${output_data}exporter_rank5.dta
* - ${output_data}exporter_rank10.dta
* - ${output_data}exporter_rank20.dta
run ${dofiles}create_ex_importer_rank.do


* Preparing data on ranking
*------------------------------------------------------------------------------
*---	Data used in main analyses
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}mexico_for_analysis_04_06_hs6_restrict.dta 
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict.dta　
* - ${output_data}mexico_for_analysis_04_08_hs6_restrict.dta　
* - ${output_data}US_for_analysis_04_06_hs6_restrict.dta　
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta 
* - ${output_data}US_for_analysis_04_08_hs6_restrict.dta　
run ${dofiles}create_rankings_main.do


* Preparing data on rankings based alternative measures (Data used in Table A24)
*------------------------------------------------------------------------------
*---	Preparing data for rankings based on total trade (Data used in Table A24)
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_total.dta
run ${dofiles}create_rankings_total.do


*------------------------------------------------------------------------------
*---	Preparing data for rankings based on price (Data used in Table A24)
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquilaprice_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}mexico_for_analysis_04_07_hs6_price_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_price_restrict.dta
run ${dofiles}create_rankings_price.do


*------------------------------------------------------------------------------
*---	Preparing data for rankings based on quality (Data used in Table A24)
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquilaprice_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}mexico_for_analysis_04_07_hs6_quality_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_quality_restrict.dta
run ${dofiles}create_rankings_quality.do


*------------------------------------------------------------------------------
*---	Preparing data for rankings using bins (Data used in Table A18)
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile5.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile10.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile20.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile5.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile10.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile20.dta
run ${dofiles}create_rankings_bin.do


*------------------------------------------------------------------------------
* Preparing data for analyses related to summary statistics (Data used in Table 1, A4, A5, A6, A9, A10, and A11)
*---  
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict.dta
* - ${rawdata}description_hs6_type.dta
* - ${output_data}importer_rank.dta
* - ${output_data}exporter_rank.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}summarystats_product_04050607.dta
* - ${output_data}summarystats_firm_04050607.dta
* - ${output_data}summarystats_regsample_product_04050607.dta
* - ${output_data}summarystats_regsample_firm_04050607.dta
* - ${output_data}product_level_characteristics.dta
* - ${output_data}import_product_level_characteristics.dta
* - ${output_data}export_product_level_characteristics.dta
run ${dofiles}prepare_summary_statistics.do


*------------------------------------------------------------------------------
* Analyzing summary statistics (For Table 1, A4, A5, A6, A9, A10, and A11)
*---  
* Input: 
* - ${output_data}summarystats_product_04050607.dta	
* - ${output_data}summarystats_firm_04050607.dta
* - ${output_data}summarystats_regsample_product_04050607.dta
* - ${output_data}summarystats_regsample_firm_04050607.dta
* - ${output_data}product_level_characteristics.dta
* - ${output_data}import_product_level_characteristics.dta
* - ${output_data}export_product_level_characteristics.dta
*
* Output: 
* - ${output}summary_statistics/summarystats0407_productlevel.dta (Table 1)
* - ${output}summary_statistics/summarystats0506_productlevel.dta (Table A4)
* - ${output}summary_statistics/summarystats0407_firmlevel.dta (Table 1)
* - ${output}summary_statistics/summarystats0506_firmlevel.dta (Table A4)
* - ${output}summary_statistics/summarystats_regsample_product_04050607.dta (Table A5)
* - ${output}summary_statistics/summarystats_regsample_firm_04050607.dta (Table A6)
* - ${output}summary_statistics/product_level_characteristics_controls.csv (Table A9)
* - ${output}summary_statistics/product_level_characteristics_diff.tex (Table A9)
* - ${output}summary_statistics/import_product_level_characteristics_obs.dta (Table A10)
* - ${output}summary_statistics/import_product_level_characteristics_control.csv (Table A10)
* - ${output}summary_statistics/import_product_level_characteristics_diff.tex (Table A10)
* - ${output}summary_statistics/export_product_level_characteristics_obs.dta (Table A11)
* - ${output}summary_statistics/export_product_level_characteristics_control.csv (Table A11)
* - ${output}summary_statistics/export_product_level_characteristics_diff.tex (Table A11)
run ${dofiles}analyze_summary_statistics.do




*------------------------------------------------------------------------------
* Preparing data for analyses related to partner changes 
*--- 
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output_data}mexico_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_09_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_09_11_hs6_restrict.dta
* - ${output_data}control_vars/mainpartnershare_importer2004.dta
* - ${output_data}control_vars/maquila_ratio_hs6_restrict.dta
* - ${output_data}control_vars/nbuyer2004.dta
* - ${output_data}control_vars/nseller2004.dta
* - ${output_data}control_vars/nexp2004.dta
* - ${output_data}control_vars/nimp2004.dta
* - ${output_data}control_vars/logtrade2004.dta
* - ${output_data}control_vars/logfirmtrade2004.dta
* - ${output_data}control_vars/exporter_nproduct.dta
* - ${output_data}control_vars/importer_nproduct.dta
* - ${output_data}mexico_for_within_across.dta
* - ${output_data}us_for_within_across.dta
run ${dofiles}prepare_partner_change.do


*------------------------------------------------------------------------------
* Analyzing partener changes (For Table 3, A1, A8, A12, A13, A14, A15, A16, A17, A18, A19, A21, A23, A24, Figure 3, and Figure A4)
*---	 
* Input: 
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}importer_rank.dta
* - ${output_data}exporter_rank.dta
* - ${output_data}US_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}US_for_analysis_09_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_07_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_08_11_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_09_11_hs6_restrict.dta
* - ${output_data}sampleweight.dta
* - ${output_data}control_vars/logfirmtrade2004.dta
* - ${output_data}control_vars/mainpartnershare_importer2004.dta
* - ${output_data}control_vars/maquila_ratio_hs6_restrict.dta
* - ${rawdata}description_hs6_type.dta
* - ${output_data}control_vars/nexp2004.dta
* - ${output_data}control_vars/nimp2004.dta
* - ${output_data}control_vars/logtrade2004.dta
* - ${output_data}control_vars/nseller2004.dta
* - ${output_data}control_vars/Naics.dta
* - ${output_data}control_vars/importer_nproduct.dta
* - ${output_data}control_vars/exporter_nproduct.dta
* - ${output_data}us_for_within_across.dta
* - ${output_data}mexico_for_within_across.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile5.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile10.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quantile20.dta
* - ${output_data}importer_rank5.dta
* - ${output_data}importer_rank10.dta
* - ${output_data}importer_rank20.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile5.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile10.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile20.dta
* - ${output_data}exporter_rank5.dta
* - ${output_data}exporter_rank10.dta
* - ${output_data}exporter_rank20.dta
* - ${output_data}US_for_analysis_04_06_hs6_restrict.dta
* - ${output_data}US_for_analysis_04_08_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_04_06_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_04_08_hs6_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_total.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_price.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_quality.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_price.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_quality.dta
*
* Output: 
* - ${output}partner_change/partner_change_0407.tex (Table 3 A)
* - ${output}partner_change/partner_change_placebo.tex (Table 3 B)
* - ${output}partner_change/partner_change_0407_weighted.tex (Table A1)
* - ${output}partner_change/partner_change_0407_probit.tex (Table A8)
* - ${output}partner_change/partner_change_0407_us_controls.tex (Table A12)
* - ${output}partner_change/partner_change_0407_mex_controls.tex (Table A13)
* - ${output}partner_change/partner_change_0407_multiproduct.tex (Table A14)
* - ${output}partner_change/partner_change_beyond_within.tex (Table A15)
* - ${output}partner_change/partner_change_multipartner_us.tex (Table A16)
* - ${output}partner_change/partner_change_multipartner_mex.tex (Table A17)
* - ${output}partner_change/partner_change_bin5.tex (Table A18)
* - ${output}partner_change/partner_change_bin10.tex (Table A18)
* - ${output}partner_change/partner_change_bin20.tex (Table A18)
* - ${output}partner_change/partner_change_cont_binding_measure.tex (Table A19)
* - ${output}partner_change/partner_change_alternative_binding_measure.tex (Table A21)
* - ${output}partner_change/gradual_partener_change.tex (Table A23)
* - ${output}partner_change/partner_change_alternative_ranking.tex (Table A24 A)
* - ${output}partner_change/partner_change_intial_ranking_apparel.png (Figure 3)
* - ${output}partner_change/partner_change_intial_ranking_non_apparel.png (Figure A4)
run ${dofiles}analyze_partner_change.do


*------------------------------------------------------------------------------
* Analyzing rank correlation (For Table A24, Figure 4 and A5)
*--- 
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict_total.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta
* - ${output_data}US_for_analysis_04_07_hs6_price_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_price_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_quality_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_quality_restrict.dta
* - ${output_data}importer_rank.dta
* - ${output_data}exporter_rank.dta
*
* Output: 
* - ${output}new_old_rank_correlation/new_old_rank_correlation_US.png (Figure 4)
* - ${output}new_old_rank_correlation/new_old_rank_correlation_US_binding.png (Figure A5)
* - ${output}new_old_rank_correlation/new_old_rank_correlation_mexico.png (Figure 4)
* - ${output}new_old_rank_correlation/new_old_rank_correlation_mexico_binding.png (Figure A5)
* - ${output}new_old_rank_correlation/new_old_rank_correlation.tex (Figure 4 and A5)
* - ${output}new_old_rank_correlation/rank_correlation_alternative_rank_measures.tex (Table A24 B)
run ${dofiles}analyze_new_old_rank_correlation.do


*------------------------------------------------------------------------------
* Analyzing exporters' exit (For Table 4 A, A20, and A22)
*--- 
* Input: 
* - ${output_data}exp_rfc_prod_textile_us20042011.dta
* - ${rawdata}description_hs6_type.dta
* - ${output_data}exp_rfc_prod_textile20002004.dta
* - ${output_data}transportista_rfclist.dta
* - ${rawdata}export1993_1999.dta
* - ${output_data}quota_mex_weight_hs6.dta
*
* Output: 
* - ${output}firm_exit/mexican_exit.tex (Table 4 A)
* - ${output}firm_exit/mexican_exit_cont_binding_measure.tex (Table A20)
* - ${output}firm_exit/mexican_exit_alternative_binding_measure.tex (Table A22)
run ${dofiles}analyze_firm_exit.do


*------------------------------------------------------------------------------
* Analyzing import growth of US importers during 2004-07 (For Table A2)
*---	 
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}importer_rank.dta
*
* Output: 
* - ${output}import_growth/import_growth.tex (Table A2)
run ${dofiles}analyze_import_growth.do


*------------------------------------------------------------------------------
* Analyzing (changes in) the number of parteners (For Table 4 B and A3)
*---	 
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}mexico_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}exporter_rank.dta
* - ${output_data}US_for_analysis_04_07_hs6_restrict.dta
* - ${output_data}importer_rank.dta
*
* Output: 
* - ${output}number_partners/change_number_of_partners_0407.tex (Table 4 B)
* - ${output}number_partners/ndam_alternative_matching_def.tex (Table A3)
run ${dofiles}analyze_number_of_partners.do


*------------------------------------------------------------------------------
* Analyzing export changes (For Table 2, and A7)
*---	 
* Input: 
* - ${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta
* - ${output_data}quota_mex_weight_hs6.dta

*
* Output: 
* - ${output}export_change/aggregate_decomposition.dta (Table 2A)
* - ${output}export_change/partner_margin_coef.tex (Table 2B)
* - ${output}export_change/aggregate_decomposition_main_partner.dta (Table 2C)
* - ${output}export_change/main_partner_margin_coef.tex (Table 2D)
* - ${output}export_change/decomposition_extensive.dta (Table A7)
run ${dofiles}analyze_export_change.do
